package sis.testio.streamtokenizer;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-29 11:48
 * @notify
 * @version 1.0
 */

import javax.xml.stream.util.StreamReaderDelegate;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;

public class TokenTest {
    public void countWordsAndNumbers(String filename) {

        StreamTokenizer sTokenizer = null;
        int wordCount = 0, numberCount = 0;

        try {

            sTokenizer = new StreamTokenizer(new FileReader(filename));

            while (sTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if (sTokenizer.ttype == StreamTokenizer.TT_WORD) {
                    System.out.println(sTokenizer.sval);
                    wordCount++;
                } else if (sTokenizer.ttype == StreamTokenizer.TT_NUMBER) {
                    System.out.println(sTokenizer.nval);
                    numberCount++;
                }
            }

            System.out.println("单词有: " + wordCount);
            System.out.println("数字有: " + numberCount);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TokenTest().countWordsAndNumbers("C:\\AgileJava\\src\\sis\\testio\\streamtokenizer\\myFile.txt");
    }
}
